package engineer.reverse.java;

import engineer.reverse.Revert;
import engineer.Engineer;
import entity_model.EntityModel;
import utils.titles.Title;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.annotation.Resource;

/**
 * Created by nuno-rafael-figueiredo on 11-07-2014.
 /*        Properties properties = new Properties();
 InputStream inputStream = JavaToEem.class.getResourceAsStream("bundle_pt.properties");
 properties.load(inputStream);
 inputStream.close();
 Enumeration<?> enumeration = properties.propertyNames();
 while (enumeration.hasMoreElements()) {
 String text = (String) properties.get(enumeration.nextElement());
 System.out.println(text + ": " + new Noun(text));
 }
 */
public class RevertJava extends Revert {
    private final Class<?> aClass;

    public RevertJava(Class<?> aClass) {
        this.aClass = aClass;
    }

    @Override
    public void execute() {
        logger.info(aClass.toString());
        setEntityModel(new EntityModel(Title.fromSnakeCase(aClass.getPackage().getName())));
        bind(new RevertClass(getEntityModel(), aClass))
                .execute();
    }

    public static void main(String... args) throws IOException {

        new RevertJava(Resource.class)
                .onFinish((observable, oldValue, newValue) -> {
                    if (!(observable instanceof RevertJava))
                        throw new RuntimeException();

                    Logger.getGlobal().info("writing");

                    EntityModel entityModel = ((RevertJava) observable).getEntityModel();
                    try (PrintWriter writer = new PrintWriter(new FileWriter(Engineer.FOLDER.getFile().getAbsoluteFile() + "/" + entityModel.getName() + ".json"))) {
                        writer.print(Engineer.toJson(entityModel));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                })
                .execute();
    }
}
